<template>
  <div class="assess-top">
  <el-dialog :title="!dataForm.brandId?'新增品牌':'编辑品牌'" :visible.sync="dialogVisible"
             @keyup.enter.native="submitForm()" label-width="150px">
    <el-form :model="dataForm" :rules="rules" ref="dataForm">
      <el-form-item label="品牌名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="品牌名称" maxlength="50" onchange="this.value=this.value.substring(0, 50)"></el-input>
      </el-form-item>
      <el-form-item label="品牌logo地址" prop="logo">
<!--         <el-input v-model="dataForm.logo" placeholder="品牌logo地址"></el-input>-->
<!--        <el-button type="primary" size="mini" @click="uploadServerLogo()">上传图片</el-button>-->
        <upload-oss-single v-model="dataForm.logo"></upload-oss-single>
      </el-form-item>
      <el-form-item label="介绍" prop="descript">
        <el-input v-model="dataForm.descript" placeholder="介绍"></el-input>
      </el-form-item>
      <el-form-item label="显示状态" prop="showStatus">
        <el-switch
          v-model="dataForm.showStatus"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>
      <el-form-item label="检索首字母" prop="firstLetter">
        <el-input v-model="dataForm.firstLetter" placeholder="检索首字母"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model.number="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm()">确 定</el-button>
    </div>
  </el-dialog>
  <!--  上传虚拟机服务器处理方式  -->
<!--  <upload-server-single ref="UploadServerSingle" :moduleName="moduleName"></upload-server-single>-->
  </div>
</template>

<script>
/* eslint-disable */
// const UploadServerSingle = () => import('@/components/upload/uploadServerSingle.vue')
const UploadOssSingle = () => import('@/components/upload/uploadOssSingle.vue')

export default {
  name:'brandAddOrUpdate',
  components: {
    // UploadServerSingle
    UploadOssSingle
  },
  props: {
    // 测试父传子页面参数  处理方法留存
    operateFlag: {
      type: String,
      default: '0' // '0'新增  '1'修改
    }
  },
  data() {
    return {
      dataForm: {
        brandId: undefined,
        name: '',
        logo: '',// 品牌logo地址
        descript: '',
        showStatus: 1,// 0不显示 1显示
        firstLetter: '',
        sort: 0 // 排序
      },
      dialogVisible:false,
      formLabelWidth: '120px',
      moduleName: '',
      rules: {
        name:[
          {required: true, message: '品牌姓名不能为空', trigger: 'blur'},
          {pattern: /^[\u4e00-\u9fa5_a-zA-Z0-9]+$/, message: '请输入中文英文数字下划线', trigger: 'blur'}
        ],
        logo: [
          { required: true, message: '品牌logo地址不能为空', trigger: 'blur' }
        ],
        showStatus: [
          { required: true, message: '显示状态[0-不显示；1-显示]不能为空', trigger: 'blur' }
        ],
        firstLetter: [
          { validator:(rule, value, callback)=>{
              if(value == ""){
                callback(new Error("检索字母不能为空"))
              }else if(!/^[a-zA-Z]$/.test(value)){
                callback(new Error("检索字母必须是在a~z或者A~Z之间"))
              }else{
                callback();
              }
            }, trigger: 'blur' }
        ],
        sort: [
          { validator:(rule, value, callback)=>{
              if(value == "" && value != 0){
                callback(new Error("检索字母不能为空"))
              }else if(!Number.isInteger(value) || value < 0){
                callback(new Error("排序必须是数字且不能小于0"))
              }else{
                callback()
              }
            }, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
  },
  methods: {
    // 初始化表单
    init(id) {
      console.log('operateFlag >>>', this.operateFlag)
      console.log('id >>>', id)

      this.dialogVisible = true
      if(!id) { // 新增
        this.dataForm.brandId = undefined
        this.dataForm.name = ''
        this.dataForm.logo = ''
        this.dataForm.descript = ''
        this.dataForm.showStatus = 1
        this.dataForm.firstLetter = ''
        this.dataForm.sort = 0
        return;
      } else {
        this.dataForm.brandId = id
        // 加载表单数据
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.$http({
            url: this.$http.adornUrl(`/product/brand/info/${this.dataForm.brandId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 200) {
              this.dataForm.name = data.data.name
              this.dataForm.logo = data.data.logo
              this.dataForm.descript = data.data.descript
              this.dataForm.showStatus = data.data.showStatus
              this.dataForm.firstLetter = data.data.firstLetter
              this.dataForm.sort = data.data.sort
            }
          })
        })
      }
    },
    // 提交表单
    submitForm() {
      // 校验表单
      this.$refs['dataForm'].validate((valid) => {
        if(valid) {
          this.$http({
            url: this.$http.adornUrl(`/product/brand/${!this.dataForm.brandId?'save':'update'}`),
            method: 'post',
            data: this.$http.adornData({
              brandId: this.dataForm.brandId || undefined,
              name: this.dataForm.name,
              logo: this.dataForm.logo,
              descript: this.dataForm.descript,
              showStatus: this.dataForm.showStatus,
              firstLetter: this.dataForm.firstLetter,
              sort: this.dataForm.sort
            }, false)
          }).then(({data}) => {
            if (data && data.code === 200) {
              this.$message({
                message: `${!this.dataForm.brandId?'新增成功':'修改成功'}`,
                type: 'success',
                duration: 1500,
                onClose: () => {
                  // 关闭弹窗
                  this.dialogVisible = false
                  // 刷新父页面
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message({
                message: data.msg,
                type: 'error'
              })
            }
          })
        }
      })
    },
    uploadServerLogo() {
      // this.$refs.UploadServerSingle.uploadServerSingleVisible = true;
    }
  }
}

</script>
